using System.Collections.Generic;
using Core;
using UnityEngine;

namespace g_gongjianta
{
    public class UISense : UIWidget
    {
        private PointerClickListener listener;
        private readonly List<Sense> list = new();

        public bool Enable;

        public override void Awake(GameObject go)
        {
            base.Awake(go);

            Enable = true;

            listener = PointerClickListener.Get(go);
            listener.OnDown += OnDownHandler;
            listener.OnUp += OnUpHandler;
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            listener.OnDown -= OnDownHandler;
            listener.OnUp -= OnUpHandler;
            listener = null;
        }

        public void Bind(Sense s)
        {
            list.Clear();
            list.Add(s);
        }

        public void Bind(IReadOnlyList<Sense> senses)
        {
            list.Clear();
            list.AddRange(senses);
        }

        private void OnDownHandler()
        {
            if (Enable == false) return;

            foreach (var sense in list)
            {
                sense.IsOn = true;
            }
        }

        private void OnUpHandler()
        {
            if (Enable == false) return;

            foreach (var sense in list)
            {
                sense.IsOn = false;
            }
        }
    }
}